package com.agileboot.attendance.controller;

import com.agileboot.attendance.domain.acceptance.AcceptanceApplicationService;
import com.agileboot.attendance.domain.acceptance.dto.AcceptanceQuery;
import com.agileboot.attendance.domain.acceptance.dto.AcceptanceRequest;
import com.agileboot.attendance.domain.acceptance.model.AcceptanceModel;
import com.agileboot.common.core.base.BaseController;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 验收单管理Controller
 */
@Tag(name = "验收单管理API", description = "验收单管理相关接口")
@RestController
@RequestMapping("/attendance/acceptance")
@RequiredArgsConstructor
@Validated
public class AcceptanceController extends BaseController {

    private final AcceptanceApplicationService acceptanceService;

    /**
     * 创建验收单
     */
    @Operation(summary = "创建验收单")
    @PostMapping
    @PreAuthorize("@permission.has('attendance:acceptance:add')")
    public ResponseDTO<Void> add(@RequestBody @Valid AcceptanceRequest request) {
        acceptanceService.createAcceptance(request);
        return ResponseDTO.ok();
    }

    /**
     * 更新验收单
     */
    @Operation(summary = "更新验收单")
    @PutMapping("/{acceptanceId}")
    @PreAuthorize("@permission.has('attendance:acceptance:edit')")
    public ResponseDTO<Void> update(@PathVariable Long acceptanceId, @RequestBody @Valid AcceptanceRequest request) {
        acceptanceService.updateAcceptance(acceptanceId, request);
        return ResponseDTO.ok();
    }

    /**
     * 删除验收单
     */
    @Operation(summary = "删除验收单")
    @DeleteMapping("/{acceptanceId}")
    @PreAuthorize("@permission.has('attendance:acceptance:delete')")
    public ResponseDTO<Void> delete(@PathVariable Long acceptanceId) {
        acceptanceService.deleteAcceptance(acceptanceId);
        return ResponseDTO.ok();
    }

    /**
     * 获取验收单详情
     */
    @Operation(summary = "获取验收单详情")
    @GetMapping("/{acceptanceId}")
    @PreAuthorize("@permission.has('attendance:acceptance:query')")
    public ResponseDTO<AcceptanceModel> getInfo(@PathVariable Long acceptanceId) {
        AcceptanceModel model = acceptanceService.getAcceptanceInfo(acceptanceId);
        return ResponseDTO.ok(model);
    }

    /**
     * 分页查询验收单列表
     */
    @Operation(summary = "分页查询验收单列表")
    @GetMapping("/list")
    @PreAuthorize("@permission.has('attendance:acceptance:list')")
    public ResponseDTO<PageDTO<AcceptanceModel>> list(AcceptanceQuery query) {
        PageDTO<AcceptanceModel> page = acceptanceService.getAcceptancePage(query);
        return ResponseDTO.ok(page);
    }
} 